package com.yami.shop.mystery.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.mystery.common.dao.CompoundMaterialsMapper;
import com.yami.shop.mystery.common.dao.CompoundMapper;
import com.yami.shop.mystery.common.dao.CompoundProdMapper;
import com.yami.shop.mystery.common.dao.CompoundRecordMapper;
import com.yami.shop.mystery.common.dto.ProdCompoundDto;
import com.yami.shop.mystery.common.model.CompoundProd;
import com.yami.shop.mystery.common.model.CompoundRecord;
import com.yami.shop.mystery.common.model.Compound;
import com.yami.shop.mystery.common.model.CompoundMaterials;
import com.yami.shop.mystery.common.service.CompoundServer;
import com.yami.shop.mystery.common.vo.CompoundMaterialsVo;
import com.yami.shop.mystery.common.vo.CompoundProdVo;
import com.yami.shop.mystery.common.vo.CompoundVo;
import com.yami.shop.security.common.bo.UserInfoInTokenBO;
import com.yami.shop.security.common.util.AuthUserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CompoundServerImpl extends ServiceImpl<CompoundMapper, Compound> implements CompoundServer {

    @Autowired
    private CompoundMapper spuCompoundMapper;
    @Autowired
    private CompoundMaterialsMapper spuCompoundDawMapper;
    @Autowired
    private CompoundProdMapper compoundProdMapper;

    @Autowired
    private CompoundRecordMapper compoundRecordMapper;


    @Override
    public PageVO<CompoundVo> getList(PageDTO pageDTO, ProdCompoundDto spuCompoundDTO) {
        Long shopId = null;
        // 判断登录类型
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        if (userInfoInTokenBO.getSysType() == 1) {
            shopId = userInfoInTokenBO.getShopId();
        }
        spuCompoundDTO.setShopId(shopId);
        return PageUtil.doPage(pageDTO, () -> spuCompoundMapper.selectPageList(spuCompoundDTO));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuCompound(CompoundVo prodCompoundVO) {
        // 判断开始结束时间
        if (prodCompoundVO.getEndTime().before(new Date())) {
            throw new YamiShopBindException("yami.mystery.compount.time");
        }
        // 新增藏品合成主表
        Compound spuCompound = BeanUtil.copyProperties(prodCompoundVO, Compound.class);
        spuCompound.setShopId(AuthUserContext.get().getShopId());
        spuCompoundMapper.insert(spuCompound);
        // 新增合成商品
        for(CompoundProdVo compountProdDto : prodCompoundVO.getCompountProdDtoList()){
            CompoundProd compountProd = BeanUtil.copyProperties(compountProdDto, CompoundProd.class);
            compountProd.setCompoundId(spuCompound.getId());
            compoundProdMapper.insert(compountProd);
        }
        // 新增合成材料
        for (CompoundMaterialsVo spuCompoundDawVO : prodCompoundVO.getCompoundDawVoList()) {
            spuCompoundDawVO.setCompoundId(spuCompound.getId());
            CompoundMaterials spuCompoundDaw = BeanUtil.copyProperties(spuCompoundDawVO, CompoundMaterials.class);
            spuCompoundDawMapper.insert(spuCompoundDaw);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpuCompound(CompoundVo prodCompoundVO) {
        // 判断开始结束时间
        if (prodCompoundVO.getStartTime().after(new Date()) || prodCompoundVO.getEndTime().before(new Date())) {
            throw new YamiShopBindException("yami.mystery.compount.time");
        }
        // 修改藏品合成主表
        Compound spuCompound = BeanUtil.copyProperties(prodCompoundVO, Compound.class);
        spuCompoundMapper.updateById(spuCompound);

        // 删除合成商品表
        compoundProdMapper.delete(new QueryWrapper<CompoundProd>().eq("compound_id",spuCompound.getId()));
        for(CompoundProdVo compountProdDto : prodCompoundVO.getCompountProdDtoList()){
            CompoundProd compountProd = BeanUtil.copyProperties(compountProdDto, CompoundProd.class);
            compountProd.setCompoundId(spuCompound.getId());
            compoundProdMapper.insert(compountProd);
        }
        // 删除合成材料表
        spuCompoundDawMapper.delete(new QueryWrapper<CompoundMaterials>().eq("compound_id", spuCompound.getId()));
        // 删除合成材料表
        for (CompoundMaterialsVo spuCompoundDawVO : prodCompoundVO.getCompoundDawVoList()) {
            spuCompoundDawVO.setCompoundId(spuCompound.getId());
            CompoundMaterials spuCompoundDaw = BeanUtil.copyProperties(spuCompoundDawVO, CompoundMaterials.class);
            spuCompoundDawMapper.insert(spuCompoundDaw);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSpuCompound(int id) {
        compoundProdMapper.delete(new QueryWrapper<CompoundProd>().eq("compound_id",id));
        spuCompoundDawMapper.delete(new QueryWrapper<CompoundMaterials>().eq("compound_id", id));
        spuCompoundMapper.delete(new QueryWrapper<Compound>().eq("id", id));
    }

    @Override
    public void updateStatus(int id, int status) {
        Compound spuCompound = spuCompoundMapper.selectById(id);
        // TODO 判断该合成规则是否有相同商品上架

        // 修改状态
        LambdaUpdateWrapper<Compound> spuCompoundUpdate = Wrappers.lambdaUpdate();
        spuCompoundUpdate.set(Compound::getStatus, status);
        spuCompoundUpdate.eq(Compound::getId, id);
        spuCompoundMapper.update(null, spuCompoundUpdate);
    }

    @Override
    public PageVO<CompoundVo> getSpuList(PageDTO pageDTO, ProdCompoundDto spuCompoundDTO) {
        Long shopId = null;
        // 判断登录类型
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        if (userInfoInTokenBO.getSysType() == 1) {
            shopId = userInfoInTokenBO.getShopId();
        }
        spuCompoundDTO.setShopId(shopId);
        return PageUtil.doPage(pageDTO, () -> spuCompoundMapper.getSpuList(spuCompoundDTO));
    }

    @Override
    public PageVO<CompoundVo> getAppSpuList(PageDTO pageDTO, ProdCompoundDto spuCompoundDTO) {
        return PageUtil.doPage(pageDTO, () -> spuCompoundMapper.getSpuList(spuCompoundDTO));
    }

    @Override
    public CompoundVo getDetail(int compoundId) {
        // 获取合成信息
        CompoundVo prodCompoundVO = spuCompoundMapper.getDetail(compoundId);
        // 获取材料信息
        List<CompoundMaterials> compoundDaws = spuCompoundDawMapper.selectList(new QueryWrapper<CompoundMaterials>().eq("compound_id", compoundId));
        List<CompoundMaterialsVo> spuCompoundDawVOS = BeanUtil.copyToList(compoundDaws, CompoundMaterialsVo.class);
        prodCompoundVO.setCompoundDawVoList(spuCompoundDawVOS);
        List<String> collect = spuCompoundDawVOS.stream().map(CompoundMaterialsVo::getMaterialsId).collect(Collectors.toList());
        // TODO 获取币的名称
        return prodCompoundVO;
    }

    @Override
    public IPage<CompoundRecord> getWaitCompoundRecordList() {
        IPage<CompoundRecord> iPage = new Page<>(1, 50);
        QueryWrapper<CompoundRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        return compoundRecordMapper.selectPage(iPage, queryWrapper);
    }
}
